package v1

import (
	"com.cyt.yunpay/app/model"
	"com.cyt.yunpay/app/service"
	"com.cyt.yunpay/library/encrypt"
	"com.cyt.yunpay/library/response"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

// 密钥配置管理对象
var SercetConfig = new(sercetConfig)

type sercetConfig struct{}

// 创建密钥配置
func (a *sercetConfig) CreateSercetConfig(r *ghttp.Request) {
	var (
		apiReq *model.CreateSercetConfigReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	rasList, err := encrypt.GetRsaKey()
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err))
	}

	params := &model.AppSecretConfig{
		AppId:          1606034340,
		AppKey:         rasList["AppKey"],
		AppSecret:      rasList["AppSecret"],
		PlatformKey:    rasList["PlatformKey"],
		PlatformSecret: rasList["PlatformSecret"],
		Status:         1,
	}
	if err := service.SercetConfig.CreateSercetConfig(params); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "创建成功")
}

// 获取应用密钥配置
func (a *sercetConfig) GetSercetConfig(r *ghttp.Request) {
	var (
		apiReq *model.GetSercetConfigReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	data, err := service.SercetConfig.GetSercetConfigByAppId(apiReq.AppId);
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	if data == nil {
		response.JsonExit(r, 1, "暂无该应用配置")
	}

	res := g.Map{
		"app_id": data.AppId,
		"app_key": data.PlatformKey,
		"app_sercet": data.AppSecret,
	}

	response.JsonExit(r, 0, "", res)
}
